// 获取Ueditor配置
var fs = require('fs');
var config_json_url = __dirname + "/ueditor/config.json";
var config_json = fs.readFileSync(config_json_url).toString();

module.exports = {
    prefix: "/ueditor",
    get: {
        "/": {
            des: {
                name: "getUeditorConfigure",
                content: "获取Ueditor配置"
            },
            method: function* () {
                var self = this;
                print("获取Ueditor配置");
                var query = self.request.query;
                console.log(config_json)
                self.body = query.callback + "(" + config_json + ");";
            }
        }
    },
    post: {
        "/": {
            des: {
                name: "UeditorUploadImage",
                content: "Ueditor上传图片"
            },
            multipart: true,
            method: function* () {
                var self = this;
                print("Ueditor上传图片");
                var query = self.request.query;
                if (query.action == "uploadimage") {
                    // 图片上传
                    try {
                        var image = self.request.body.files.upfile;
                        var img = yield qiniuUpload(image.name, image.path);
                        var result = {
                            "url": qiniu_server_url + img.key,
                            'title': img.key,
                            'original': img.key,
                            'state': 'SUCCESS'
                        };
                        // self.body = function() {
                        //     if (query.callback) {
                        //         return query.callback + "(" + JSON.stringify(result) + ")";
                        //     } else {
                        //         return JSON.stringify(result);
                        //     };
                        // }();;
                        self.body = result;
                    } catch (e) {
                        self.body = printError(e);
                    };
                }
            }
        }
    }
}